#include "kcppacket.h"

void KCPPacket::EncodeFull() {
	if(data != nullptr && data->size() > 0) {
		fullData.resize(data->size()+4+1);
		if(isAck) {
			fullData[0] = 1;
		}else {
			fullData[0] = 0;
		}
		unsigned char *fd = &fullData[0];
		unsigned char *bd = (unsigned char*)&sn;
		memcpy(fd+1, bd, 4);
		memcpy(fd+5, &(*data)[0], data->size());
	}else {
		fullData.resize(0+4+1);
		if(isAck) {
			fullData[0] = 1;
		}else {
			fullData[0] = 0;
		}
		unsigned char *fd = &fullData[0];
		unsigned char *bd = (unsigned char*)&sn;
		memcpy(fd+1, bd, 4);
	}
}

void KCPPacket::DecodeData(std::vector<unsigned char> &rcData) {
	data = shared_ptr<std::vector<unsigned char> >(new std::vector<unsigned char>);
	
	isAck = rcData[0] == 1;
	fullData = rcData;
	sn = *((uint32_t*)&rcData[1]);
	data->resize(rcData.size()-5);
	if(data->size() > 0) {
		memcpy(&(*data)[0], &rcData[5], rcData.size()-5);
	}
}